package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PmsProductAttributePage;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.PmsProductAttribute;
import com.genesis.mall.entity.PmsProductAttributeCategory;
import com.genesis.mall.service.IPmsProductAttributeCategoryService;
import com.genesis.mall.service.IPmsProductAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/productAttribute")
@Slf4j
public class PmsProductAttributeController {
    @Autowired
    private IPmsProductAttributeService iPmsProductAttributeService;

    @Autowired
    private IPmsProductAttributeCategoryService productAttributeCategoryService;

    /**
     * 分页查询：商品属性列表
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list/{parentId}")
    public Result<Map<String,Object>> getPage(@PathVariable int parentId, Integer pageNum, Integer pageSize, int type){
        if(pageNum != null || pageSize != null) {
            //分页构造器
            Page<PmsProductAttribute> pageInfo = new Page(pageNum, pageSize);
            //条件构造器
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProductAttribute::getType, type).eq(PmsProductAttribute::getProductAttributeCategoryId, parentId);
            Page<PmsProductAttribute> page = iPmsProductAttributeService.page(pageInfo, queryWrapper);
            //创建对象
            PmsProductAttributePage<PmsProductAttribute> productAttributePage = new PmsProductAttributePage();
            BeanUtils.copyProperties(page, productAttributePage);

            Map<String,Object> map = new HashMap<>();
            map.put("productAttributePage",productAttributePage);

            return Result.success(map);
        }else{
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProductAttribute::getType, type).eq(PmsProductAttribute::getProductAttributeCategoryId, parentId);
            List<PmsProductAttribute> list = iPmsProductAttributeService.list(queryWrapper);

            Map<String,Object> map = new HashMap<>();
            map.put("list",list);

            return Result.success(map);

        }

    }

    /**
     * 添加商品属性列表中的属性
     * @return
     */
    @PostMapping("/create")
    public Result<String> save(@RequestBody PmsProductAttribute pmsProductAttribute){
        log.info("添加商品属性的信息");
        //先查出商品类型中对于的分类数据
        PmsProductAttributeCategory attributeCategory = productAttributeCategoryService.getById(pmsProductAttribute.getProductAttributeCategoryId());

        if (pmsProductAttribute.getType()==0){
            Integer attributeCount = attributeCategory.getAttributeCount();
            attributeCategory.setAttributeCount(attributeCount+1);
        }else{
            Integer paramCount = attributeCategory.getParamCount();
            attributeCategory.setParamCount(paramCount+1);
        }
        productAttributeCategoryService.updateById(attributeCategory);
        iPmsProductAttributeService.save(pmsProductAttribute);
        return Result.success("商品属性添加成功");
    }

    /**
     * 根据id回显要修改的属性
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<PmsProductAttribute> selectById(@PathVariable int id){
        log.info("回显id为："+id+"的属性");
        PmsProductAttribute pms = iPmsProductAttributeService.getById(id);
        return Result.success(pms);
    }

    @PostMapping("/update/{id}")
    public Result<String> update(@PathVariable int id,@RequestBody PmsProductAttribute pmsProductAttribute){
        log.info("根据id修改商品的属性列表中的信息");

        iPmsProductAttributeService.updateById(pmsProductAttribute);
        return Result.success("修改成功");
    }

    /**
     * 批量删除商品的属性列表中的信息
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestParam("ids") List<Integer> ids){
        log.info("根据id删除商品的属性信息");
        PmsProductAttribute pmsProductAttribute = null;
        for (Integer id : ids) {
            pmsProductAttribute = iPmsProductAttributeService.getById(id);
        }

        //获取商品类型的对象
        PmsProductAttributeCategory attributeCategory = productAttributeCategoryService.getById(pmsProductAttribute.getProductAttributeCategoryId());
        if(pmsProductAttribute.getType() == 1){
            //计算数量
            Integer paramCount = attributeCategory.getParamCount();
            attributeCategory.setParamCount(paramCount- ids.size());
        }else{
            Integer attributeCount = attributeCategory.getAttributeCount();
            attributeCategory.setAttributeCount(attributeCount - ids.size());
        }
        productAttributeCategoryService.updateById(attributeCategory);
        LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsProductAttribute::getId,ids);
        iPmsProductAttributeService.remove(queryWrapper);

        return Result.success("删除成功");
    }

    @GetMapping("/attrInfo/{id}")
    public Result<String> attrInfo(){


        return null;
    }

    @GetMapping("/update/{id}")
    public Result<String> update(int id){
        log.info("修改商品的分类");

        return null;
    }

}
